clear *  
macro drop _all
set more off
set type double, perm
set scheme s1color

//
//  ---- Master file
//

// file locations
// - Set scripts and data paths

	// set root:
	global root /Users/bensand/Library/CloudStorage/Dropbox/Projects/Union_Rep_Package/

	// locations
	global scripts ${root}/Programs
	global cps     ${root}/Programs/01_cps
	global may     ${root}/Programs/01_May_matching
	global ipums   ${root}/Programs/01_IPUMS_CPS
	global nlrb    ${root}/Programs/01_nlrb
	global cbp     ${root}/Programs/01_cbp
	global psd     ${root}/Programs/01_partisanship
	global td      ${scripts}/02_preprocess
	global est     ${scripts}/03_analysis/STATA_scripts
	global Rest     ${scripts}/03_analysis/Rscripts
	
	global raw ${root}/raw_data
	global wd  ${root}/working_data
	global temp ${root}/temp
	
	global tables ${root}/results/tables
	global figures ${root}/results/figures
	
	// create directories
	cap mkdir ${root}/results
	cap mkdir $tables
	cap mkdir $figures
	cap mkdir $wd
	cap mkdir $wd/CBP
	cap mkdir $wd/cps
	cap mkdir $wd/decomp_dynamic
	cap mkdir $wd/keyvars
	cap mkdir $wd/May_matching
	cap mkdir $wd/ipums_ind_trans
	cap mkdir $wd/nlrb
	cap mkdir $wd/partisanship
	cap mkdir $root/temp


// Parameters
	/*************************************
	Set industry and geography for analysis
	ind: detind
	area: city_plus2
	**************************************/
	
global ind     = "detind"
global area    = "city_plus2"
global maxyear = 5

// Table parameters
	// - What first stage statistic (F or p)?
	global FSTAT p

	// - What cluster level?
	global clustlevel   clusters 
	
	*global clustlbl "city-year and industry-year"
	global clustlbl "city-year"
	
	// custom scripts.
	do "$scripts/02_estaddfirst.do"	 // first stage extraction
	do "$scripts/esttab.ado"         // Table creation

/*********************
Data Processing
**********************/

// Morg/may CPS Extraction and clean
do "${cps}/cps_data.do"
 
// IPUMS CPS Extraction
do "${ipums}/cps_00055_extraction.do"

// May matching
do "${may}/00_runner.do"

// NLRB data - Cleaining
do "${nlrb}/NLRB Master.do"

// CBP Extract and Clean 
do "${cbp}/Run_extract.do"

// Partisanship
do "${psd}/Partisanship_run.do"

// Other Processing 
do ${td}/N_counts.do  
do ${td}/ER.do        
do ${td}/union_elections.do  
do ${td}/cps_transitions.do 

// table data
local pexp 0	
foreach groupCond in  ///
       "All         age >= 20" ///
		"Men        female == 0" ///
		"Women      female == 1" ///
		"Young      age >= 20 & age <= 35" ///
		"Old        age >= 36 & age <= 55" ///
		"Low        educ <= 2" ///
		"High       educ > 2" ///
		"EF_young_low   age >= 20 & female == 0" ///
		"EF_young_high  age >= 20 & female == 0" ///
		"EF_old_low     age >= 20 & female == 0" ///
		"EF_old_high    age >= 20 & female == 0" ///
	{

    * 1) Grab the first word as the label
    local label = word("`groupCond'", 1)

    * 2) Capture condition
    local cond = trim( ///
					  subinstr("`groupCond'", "`label'", "", 1) ///
					 )

    di as text "Label = `label', Condition = `cond'"
	
	* 3) Run table data
qui	do "${td}/Table_Data.do" "`cond'" "`label'" 0 `pexp'
qui	do "${td}/Table_Data.do" "`cond'" "`label'" 6 `pexp'
	
	}

do "${td}/Table_data_union.do" "age >= 20" "All" "0"

// 1) perform the decomposion
	foreach grp in All Men Women EF_young_low EF_young_high EF_old_low EF_old_high {  
		
	display "Starting group: `grp'"
    timer clear 1
    timer on 1

		// Decomp
		do "$est/Decomposition_Data.do" `grp' "no" 6 20  
			
		// Chained
		do "$est/Decomposition_Data_Chain.do" `grp' "no" 6 20 1 dynamic
		do "$est/Decomposition_Data_Chain.do" `grp' "no" 6 20 2 dynamic
		do "$est/Decomposition_Data_Chain.do" `grp' "no" 6 20 3 dynamic
		
	timer off 1
    timer list 1
    display "Finished group: `grp'"
		
		}


 *************************************************************************
 * Inequality figure for revision
 * Data for Inequality Figure
 *************************************************************************

 
local pexp 10
foreach groupCond in ///
    "Men10          female == 0" ///
    "Women10        female == 1" ///
    "Low10          educ <= 2" ///
    "High10         educ > 2" ///
    "LowMen10       educ <= 2 & female == 0" ///
    "LowWomen10     educ <= 2 & female == 1" ///
    "HighMen10      educ > 2 & female == 0" ///
    "HighWomen10    educ > 2 & female == 1" {

    * 1) Grab the first word as the label
    local label = word("`groupCond'", 1)

    * 2) Capture condition
    local cond = trim( ///
					  subinstr("`groupCond'", "`label'", "", 1) ///
					 )

    di as text "Label = `label', Condition = `cond'"

    * 3) Run table data
	qui	do "${td}/Table_Data.do" "`cond'" "`label'" 6 `pexp'
}   

foreach grp in Men10 Women10  Low10 High10 LowMen10 LowWomen10 HighMen10 HighWomen10  { 
			do "$est/Decomposition_Data.do" `grp' "no" 6 20
	}		

// Tables
//

	// -------------------------------------------
	// Paper: Tables 1 - 3
	// -------------------------------------------

	// Table 1 - Non-Union Wages and Outside Options
	do "${est}/Table_1.do" all  0 	propic
	
	// Table 2 - subsample anlaysis
	do "${est}/Table_2.do" 0 	

	// Table 3 - Decomposition
	do "${est}/Table_3.do" 

	// -------------------------------------------
	// Appendix: Tables 4 - 8
	// -------------------------------------------
		
	// Table 4 - Non-Union Wages and Outside Options: Appendix version with alternate selection cnt.
	do "${est}/Table_1.do" all  0 	cert

	// Table 5: Bias Simulation (Appendix Table 5 and Figure 5)
	do "${est}/Table_5_Figure_5_Appendix.do"
	
	// Table 6:  Non-Union Wages and Outside Options: OLS and 2SLS Estimates
	do "${est}/Table_6_Appendix.do"
		
	// Table 7: Change in Union Premium and Threat
	do "${est}/Table_7_Appendix.do"
	
	// Table 8: Changed Decomposition
	do "${est}/Table_3_chain.do"

//	
// Figures
//

	// -------------------------------------------
	// Paper: Figures 2 - 6
	// -------------------------------------------
	
	/* 
	Figures are created in R, with script below.
	Data comes from STATA code above,
	or from the following:
	*/
	
	// data for figure 2, 3 & 6, Appendix figure 3
	do "${est}/Figure_2_data.do"      			// Transitions to Union Jobs data
	do "${est}/Figure_3_data.do"      			// Components of Decomposition data
	do "${est}/Figure_6_data.do"     			// Append Inquality data
	do "${est}/Appendix_Figure_3_data.do"       // union trends
	
	* -> This figures created by R script below.
	// Figure 2: Transitions to union jobs
	// Figure 3: Components of Decomposition
	// Figure 4: Average Wage Decomposition
	// Figure 5: Figure 5: Implications for Wage Inequality
	// Figure 6: Figure 6: Decomposition components: Non-union workers
	// Appendix: Figures 3 & 4
	
	// Either just open R and run the master file directly 
	// or replace /usr/local/bin/Rscript with the full path to Rscript on your machine if different. 
	// to find it, run 'which Rscript' in terminal.
	
	shell /usr/local/bin/Rscript "$root/Master_for_R_Figures.R"
	














